package com.bitcat.android.datastoragedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.bitcat.android.datastoragedemo.databinding.ActivitySqliteBinding;

public class SqliteActivity extends AppCompatActivity {

    DatabaseHelper databaseHelper;
    private SQLiteDatabase db;

    private ActivitySqliteBinding binding;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySqliteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        databaseHelper = new DatabaseHelper(this);
        db = databaseHelper.getWritableDatabase();
    }

    public void addUser(View view) {
        String name = binding.nameEt.getText().toString();
        String phone = binding.phoneEt.getText().toString();
        if (name.isEmpty() || phone.isEmpty()) {
            Toast.makeText(this, "姓名或者电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("phone", phone);

        db.insert("user", null, values);

        // 清空输入框
        binding.nameEt.setText("");
        binding.phoneEt.setText("");
        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("Range")
    public void findAll(View view) {
        binding.contentTv.setText("");

        // 查询所有用户
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                 String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                binding.contentTv.append("姓名：" + name + "，电话：" + phone + "\n");
            } while (cursor.moveToNext());
        }
    }

    public void updateUser(View view) {
        //根据姓名更新电话
        String name = binding.nameEt.getText().toString();
        String phone = binding.phoneEt.getText().toString();
        if (name.isEmpty() || phone.isEmpty()) {
            Toast.makeText(this, "姓名或者电话不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        ContentValues values = new ContentValues();
        values.put("phone",phone);

        db.update("user",values,"name=?",new String[]{name});
        Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
    }

    public void deleteUser(View view) {
        //根据姓名删除用户
        String name = binding.nameEt.getText().toString();
        if (name.isEmpty()) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        db.delete("user","name=?",new String[]{name});
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }
}